package com.limit.service.event;

import com.limit.aspect.core.AnnotationAspectTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @title: EventListener
 * @Author xu-xiang
 * @Date: 2024/3/28 17:09
 * @description:
 */
@Component
public class EventListenerService {

    private static final Logger logger = LoggerFactory.getLogger(EventListenerService.class);

    @Resource
    private ApplicationContext applicationContext;

    @EventListener(value = {ContextRefreshedEvent.class})
    public void event(ContextRefreshedEvent event) {
        Map<String, AnnotationAspectTemplate> beans = applicationContext.getBeansOfType(AnnotationAspectTemplate.class);
        if (!CollectionUtils.isEmpty(beans)) {
            logger.warn("系统已加载的限流算法有:\n{}", String.join(",\n", beans.keySet()));
            logger.warn("点击这里直接访问:{}", "http://localhost:8080/limit/query");
        }
    }
}
